//
//  LoginViewController.m
//  忘不了
//
//  Created by rimi on 16/7/7.
//  Copyright © 2016年 handuan. All rights reserved.
//
#import "CheckWeb.h"
#import "LoginViewController.h"
#import "BaseRequest.h"
#import "SDAutoLayout.h"
#import "HomeViewController.h"
#import "registerViewController.h"
#import "findPasswordViewController.h"
#import "UserModel.h"
#import "MBProgressHUD.h"
#import "UMSocial.h"
#import "BaseRequest.h"
@interface LoginViewController ()<UITextFieldDelegate>
@property (nonatomic,strong)UITextField *userName;
@property (nonatomic,strong)UITextField *password;
@property (nonatomic,strong)UIButton *loginButton;
@property (nonatomic,strong)UIButton *registerButton;
@property (nonatomic,strong)UIButton *forgetPassword;
@property (nonatomic,strong)UILabel *warningLabel;
@property (nonatomic,strong)UIView *lineOne;
@property (nonatomic,strong)UIView *lineTwo;
@property (nonatomic,strong)UILabel *qqLabel;
@property (nonatomic,strong)UIButton *qqButton;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
                                                         forBarMetrics:UIBarMetricsDefault];//隐藏导航栏返回按钮自带的文字
    self.title = @"登录";
    //设置导航栏标题颜色、字体大小等
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor brownColor]}];
    
    UIImageView *backGroudView = [[UIImageView alloc]initWithFrame:self.view.frame];
    backGroudView.image = [UIImage imageNamed:@"s.jpg"];
    [self.view addSubview:backGroudView];
    
    [self loadUI];
    
}

- (void)loadUI {
    _userName = [[UITextField alloc]init];
    _userName.delegate = self;
    _userName.placeholder = @"  账号";
    _userName.textColor = [UIColor blackColor];
    _userName.font = [UIFont systemFontOfSize:20];
    _userName.layer.cornerRadius = 25;
    _userName.layer.masksToBounds = YES;
    _userName.clearButtonMode = UITextFieldViewModeWhileEditing;
    _userName.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
    [self.view addSubview:_userName];
    _userName.sd_layout
    .centerXIs(self.view.frame.size.width/2)
    .centerYIs(self.view.frame.size.height/2-80)
    .leftSpaceToView(self.view,60)
    .rightSpaceToView(self.view,60)
    .heightIs(50);
    
    _password = [[UITextField alloc]init];
    _password.delegate = self;
    _password.placeholder = @"  密码";
    _password.textColor = [UIColor blackColor];
    _password.secureTextEntry = YES;
    _password.font = [UIFont systemFontOfSize:20];
    _password.layer.cornerRadius = 25;
    _password.layer.masksToBounds = YES;
    _password.clearButtonMode = UITextFieldViewModeWhileEditing;
    _password.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
    [self.view addSubview:_password];
    _password.sd_layout
    .centerXEqualToView(_userName)
    .centerYIs(self.view.frame.size.height/2)
    .leftEqualToView(_userName)
    .rightEqualToView(_userName)
    .topSpaceToView(_userName,30)
    .heightRatioToView(_userName,1);
    
    _warningLabel = [[UILabel alloc]init];//登录失败提示label
    _warningLabel.alpha = 0;
    _warningLabel.text = @"";
    _warningLabel.textAlignment = NSTextAlignmentCenter;
    _warningLabel.textColor = [UIColor redColor];
    _warningLabel.font = [UIFont systemFontOfSize:18];
    [self.view addSubview:_warningLabel];
    _warningLabel.sd_layout
    .centerXEqualToView(_password)
    .leftEqualToView(_password)
    .rightEqualToView(_password)
    .topSpaceToView(_password,10)
    .heightRatioToView(_password,1);
    
    _loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [_loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _loginButton.backgroundColor = [UIColor colorWithRed:0.902 green:0.3451 blue:0.3686 alpha:1.0];
    _loginButton.layer.cornerRadius = 10;
    _loginButton.layer.masksToBounds = YES;
    [_loginButton addTarget:self action:@selector(loginButtonTaped) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_loginButton];
    _loginButton.sd_layout
    .centerXEqualToView(_password)
    .leftEqualToView(_password)
    .rightEqualToView(_password)
    .bottomSpaceToView(self.view,160)
    .heightRatioToView(_password,1);
    
    _registerButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_registerButton setTitle:@"立即注册" forState:UIControlStateNormal];
    [_registerButton setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
    _registerButton.backgroundColor = [UIColor clearColor];
    _registerButton.layer.cornerRadius = 10;
    _registerButton.layer.masksToBounds = YES;
    [_registerButton addTarget:self action:@selector(registerButtonTaped) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_registerButton];
    _registerButton.sd_layout
    .rightSpaceToView(self.view,60)
    .bottomSpaceToView(self.view,50)
    .widthRatioToView(_loginButton,0.3)
    .heightRatioToView(_loginButton,0.7);
    
    _forgetPassword = [UIButton buttonWithType:UIButtonTypeSystem];
    [_forgetPassword setTitle:@"忘记密码" forState:UIControlStateNormal];
    [_forgetPassword setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
    _forgetPassword.backgroundColor = [UIColor clearColor];
    [_forgetPassword addTarget:self action:@selector(forgetPasswordTaped) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_forgetPassword];
    _forgetPassword.sd_layout
    .leftSpaceToView(self.view,60)
    .bottomSpaceToView(self.view,50)
    .widthRatioToView(_loginButton,0.3)
    .heightRatioToView(_loginButton,0.7);
    
    _lineOne = [[UIView alloc]init];
    _lineOne.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_lineOne];
    _lineOne.sd_layout
    .leftSpaceToView(self.view,10)
    .widthIs(self.view.frame.size.width*0.3)
    .bottomSpaceToView(self.forgetPassword,30)
    .heightIs(1);
    
    _lineTwo = [[UIView alloc]init];
    _lineTwo.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_lineTwo];
    _lineTwo.sd_layout
    .rightSpaceToView(self.view,10)
    .widthIs(self.view.frame.size.width*0.3)
    .bottomSpaceToView(self.registerButton,30)
    .heightIs(1);
    
    _qqLabel = [[UILabel alloc]init];
    _qqLabel.text = @"QQ登录";
    _qqLabel.textColor = [UIColor blackColor];
    _qqLabel.textAlignment = NSTextAlignmentCenter;
    _qqLabel.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:_qqLabel];
    _qqLabel.sd_layout
    .widthIs(self.view.frame.size.width*0.2)
    .heightIs(self.view.frame.size.width*0.07)
    .bottomSpaceToView(self.registerButton,16)
    .centerXIs(self.view.frame.size.width/2);
    
    _qqButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_qqButton setBackgroundImage:[UIImage imageNamed:@"QQ"] forState:UIControlStateNormal];
    [_qqButton addTarget:self action:@selector(qqButtonTaped) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_qqButton];
    _qqButton.sd_layout
    .widthIs(self.view.frame.size.width*0.1)
    .heightEqualToWidth()
    .topSpaceToView(self.qqLabel,5)
    .centerXIs(self.view.frame.size.width/2);
}

- (void)qqButtonTaped {
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        if (response.responseCode == UMSResponseCodeSuccess) {
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:snsPlatform.platformName];
            UserModel *user = [UserModel shareUserModel];
            user.isLogin = YES;
            user.userNmae = snsAccount.userName;
            user.image = snsAccount.iconURL;
            user.isSan = YES;
            [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"userInfo"];
            [self.navigationController popViewControllerAnimated:YES];
        }});
}

- (void)loginButtonTaped {
    if ([_userName.text  isEqual: @""] || [_password.text  isEqual: @""]) {
        UIAlertController *tips = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请填写完整" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [tips addAction:sure];
        [self presentViewController:tips animated:YES completion:nil];
    } else {
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        [self performSelector:@selector(warningShow) withObject:nil afterDelay:1];
    }
}
- (void)warningShow{
    BOOL isWeb = [CheckWeb checkWang];
    if (isWeb == NO) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        _warningLabel.alpha = 1;
        _warningLabel.text = @"请检查您的网络";
        [self performSelector:@selector(dismiss) withObject:nil afterDelay:2.0];
    }else{
        [BaseRequest startLoginWithUserName:self.userName.text password:self.password.text completionhanlder:^(Response *response) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            if (response.isSuccess) {
                UserModel *user = [UserModel shareUserModel];
                user.isLogin = YES;
                user.isSan = NO;
                [self.navigationController popViewControllerAnimated:YES];
                //存储账号密码
                [[UserModel shareUserModel] storeUserName:_userName.text Password:_password.text];
            }else{
                _warningLabel.alpha = 1;
                _warningLabel.text = @"账号或密码错误";
                [self performSelector:@selector(dismiss) withObject:nil afterDelay:2.0];
                
                
            }
        }];

    }
}
- (void)registerButtonTaped {
    registerViewController *registerVC = [[registerViewController alloc]init];
    [self.navigationController pushViewController:registerVC animated:YES];
}

- (void)forgetPasswordTaped {
    findPasswordViewController *findVC = [[findPasswordViewController alloc]init];
    [self.navigationController pushViewController:findVC animated:YES];
}

//文本框限定最大长度
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (range.location >= 15) {
        return NO;
    }
    return YES;
}

//dismiss 警告提示框
- (void)dismiss
{
    _warningLabel.alpha = 0;
    _warningLabel.text = @"";
    [self dismissViewControllerAnimated:YES completion:nil];
}

//点空白回收键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


@end
